package com.houtai.controller.wiki;

import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.Page;
import com.houtai.base.ResultVo;
import com.houtai.bean.dto.wiki.*;
import com.houtai.bean.vo.dict.DictListResult;
import com.houtai.bean.vo.wiki.TdWikiDetailVo;
import com.houtai.bean.vo.wiki.TdWikiListVo;
import com.houtai.bean.vo.wiki.TdWikiUserDetailVo;
import com.houtai.bean.vo.wiki.TdWikiUserListVo;
import com.houtai.bean.vo.wiki.collection.TdWikiCollectionInsertDto;
import com.houtai.bean.vo.wiki.praise.TdWikiPraiseInsertDto;
import com.houtai.service.dict.DictService;
import com.houtai.service.wiki.WikiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value="百科接口", tags={"百科接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class WikiContorller {
    @Autowired
    private WikiService wikiService;

    @Autowired
    private DictService dictService;


    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="百科类别列表", notes="百科类别列表")
    @RequestMapping(value = "/wiki/type/list", method = RequestMethod.POST)
    public DictListResult type() {
        return dictService.findDictByType("entry_type");
    }


    @ApiOperation(value="搜索百科", notes="搜索百科")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/wiki/list")
    public TdWikiListVo list(@ApiParam(value = "搜索百科所需参数") @Valid @RequestBody TdWikiListDto dto, BindingResult result) {
        TdWikiListVo retInfo = new TdWikiListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = wikiService.wikiList(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="百科详情", notes="百科详情")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/wiki/detail")
    public TdWikiDetailVo detail(@ApiParam(value = "百科详情所需参数") @Valid @RequestBody TdWikiDetailDto dto, BindingResult result,
                                 @RequestHeader String token) {
        TdWikiDetailVo retInfo = new TdWikiDetailVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = wikiService.wikiDetail(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="收藏百科", notes="收藏百科")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/wiki/collection")
    public ResultVo collection(@ApiParam(value = "收藏百科所需参数") @Valid @RequestBody TdWikiCollectionInsertDto dto, BindingResult result,
                               @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = wikiService.collection(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="取消收藏百科", notes="取消收藏百科")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/wiki/collection/cancel")
    public ResultVo collectionCancel(@ApiParam(value = "取消收藏百科所需参数") @Valid @RequestBody TdWikiCollectionInsertDto dto, BindingResult result,
                                     @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = wikiService.collectionCancel(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="点赞百科", notes="点赞百科")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/wiki/praise")
    public ResultVo praise(@ApiParam(value = "点赞百科所需参数") @Valid @RequestBody TdWikiPraiseInsertDto dto, BindingResult result,
                           @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = wikiService.praise(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="取消点赞百科", notes="取消点赞百科")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/wiki/praise/cancel")
    public ResultVo praiseCancel(@ApiParam(value = "取消点赞百科所需参数") @Valid @RequestBody TdWikiPraiseInsertDto dto, BindingResult result,
                                 @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = wikiService.praiseCancel(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="添加百科", notes="添加百科")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/wiki/insert")
    public ResultVo insertWiki(@ApiParam(value = "添加百科所需参数") @Valid @RequestBody TdWikiInsertDto dto, BindingResult result,
                               @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = wikiService.insertWiki(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="编辑百科", notes="编辑百科")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/wiki/update")
    public ResultVo updateWiki(@ApiParam(value = "编辑百科所需参数") @Valid @RequestBody TdWikiUpdateDto dto, BindingResult result,
                               @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = wikiService.updateWiki(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="删除百科", notes="删除百科")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/wiki/delete")
    public ResultVo deleteWiki(@ApiParam(value = "删除百科所需参数") @Valid @RequestBody TdWikiDeleteDto dto, BindingResult result,
                               @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = wikiService.deleteWiki(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="删除百科模块", notes="删除百科模块")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/wiki/con/delete")
    public ResultVo deleteWikiCon(@ApiParam(value = "删除百科模块所需参数") @Valid @RequestBody TdWikiConDeleteDto dto, BindingResult result,
                                  @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = wikiService.deleteWikiCon(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="我上传的百科列表", notes="我上传的百科列表")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/wiki/user/list")
    public TdWikiUserListVo list(@ApiParam(value = "我上传的百科列表所需参数") @Valid @RequestBody Page dto, BindingResult result,
                                 @RequestHeader String token) {
        TdWikiUserListVo retInfo = new TdWikiUserListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = wikiService.wikiUserList(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="我的百科详情", notes="我的百科详情")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/wiki/user/detail")
    public TdWikiUserDetailVo list(@ApiParam(value = "我的百科详情所需参数") @Valid @RequestBody TdWikiDetailDto dto, BindingResult result,
                                   @RequestHeader String token) {
        TdWikiUserDetailVo retInfo = new TdWikiUserDetailVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = wikiService.wikiUserDetail(dto, token);
        }
        return retInfo;
    }
}
